<?
// Generate pagination links given a page number and the last page number.
// The page number is the page to be displayed.
// The last page number is obtained by the total number of rows divided by 
// the number of rows to be shown per page.
function paginate($pageNum, $last) {
	echo "<div id='paginate'>";
	if($pageNum == 1) {
		echo "| <span style='font-weight: bold;'>$pageNum</span> |";
	}
	// Append eventID for certain pages that need it
	if(basename($_SERVER["PHP_SELF"]) == "eventResults.php") {
		$val = "&eventID=" . $_GET['eventID'];
	}
	// Show the current page and links to the previous 2 pages.
	if($pageNum != 1) {
		echo "<a href='" . $_SERVER['PHP_SELF'] . "?pagenum=1$val'>[First]</a> ";
		$prev1 = $pageNum - 1;
		$prev2 = $pageNum - 2;
		
		// If there are pages between the first page and $prev2 then show
		// a "..." link to indicate that there are more pages in between them.
		if(($prev2 - 1) > 0) {
			echo "<a href='" . $_SERVER['PHP_SELF'] . "?pagenum=" . ($prev2 - 1) . $val . "'>...</a> ";
		}
		// Show the $prev2 link only if it is within range.
		if($prev2 >= 1) {
			echo " | <a href='" . $_SERVER['PHP_SELF'] . "?pagenum=$prev2$val'>$prev2</a> ";
		}
		echo "| <a href='" . $_SERVER['PHP_SELF'] . "?pagenum=$prev1$val'>$prev1</a> | 
			<span style='font-weight: bold;'>$pageNum</span> |";
	}
	
	// Show the current page and links to the next 2 pages.
	if($pageNum != $last) {
		$next1 = $pageNum + 1;
		$next2 = $pageNum + 2;
		echo "
			<a href='" . $_SERVER['PHP_SELF'] . "?pagenum=$next1$val'>$next1</a> |";
		// Show the $next2 link only if it is in range.
		if($next2 <= $last) {
			echo " <a href='" . $_SERVER['PHP_SELF'] . "?pagenum=$next2$val'>$next2</a> |";
		}
		// If there are pages between $next2 and the last page then show
		// a "..." link to indicate that there are more pages in between them.
		if(($last - $next2) > 0) {
			echo " <a href='" . $_SERVER['PHP_SELF'] . "?pagenum=" . ($next2 + 1) . "'>...</a>";
		}
		echo " <a href='" . $_SERVER['PHP_SELF'] . "?pagenum=$last$val'>[Last]</a>";
	}
	echo "</div>";
}
?>